<?php
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use app\common\model\TemplateGroupModel;
use app\common\model\TemplateCategoryModel;
use app\common\logic\admin\TemplateGroup;
use app\common\logic\EwmLogic;
/**
 * 套件列表
 */
class TemplateGroupController extends AdminBaseController
{
	//模板分类列表
	public function index(){
		$data = TemplateCategoryModel::all(['level'=>2]);
		$this->assign('data',$data);
		return $this->fetch();
	}
	//编辑主题活动和模板分类
	public function editCategory(){
		$param = $this->request->param();
		$data = TemplateCategoryModel::get($param['id']);
		$this->assign('data',$data);
		return $this->fetch();
	}
	//提交
	public function toEditCategory(){
		$param = $this->request->param();
		TemplateCategoryModel::where('category_id',$param['id'])->update(['name'=>$param['name']]);
		TemplateCategoryModel::where('category_id',$param['parent_id'])->update(['name'=>$param['parent_name']]);
		$this->success('操作成功','index');
	}
	//添加套件
    public function add(){
    	$param = $this->request->param();
    	$data = TemplateCategoryModel::all(['level'=>2]);
		$this->assign('data',$data);
		return $this->fetch();
    }
    //提交添加套件
    public function toAdd(TemplateGroup $TemplateGroup){
    	$param = $this->request->param();
    	$rs = $TemplateGroup->save($param);
    	if($rs['code']){
    		$this->success('添加成功',"groupIndex?id=".$param['post']['category_id']);
    	}else{
    		$this->error($rs['msg']);
    	}
    }
    //套件列表
    public function groupIndex(){
    	$param = $this->request->param();
    	$data = TemplateGroupModel::where('category_id',$param['id'])->order('group_id desc')->paginate(20,false,['query'=>$param]);
    	$this->assign('data',$data);
    	$this->assign('page',$data->render());
    	return $this->fetch();
    }
    //编辑
    public function edit(){
    	$param = $this->request->param();
    	$data_category = TemplateCategoryModel::all(['level'=>2]);
    	$data = TemplateGroupModel::get($param['id']);
    	$this->assign('data',$data);
    	$this->assign('data_category',$data_category);
    	return $this->fetch();
    }
    //提交编辑
    public function toEdit(TemplateGroup $TemplateGroup){
    	$param = $this->request->param();
    	$rs = $TemplateGroup->save($param,2);
    	if($rs['code']){
    		$this->success('编辑成功',"groupIndex?id=".$param['post']['category_id']);
    	}else{
    		$this->error($rs['msg']);
    	}
    }
    //预览
    public function look(EwmLogic $lEwm){
    	$param = $this->request->param();
    	$data = TemplateGroupModel::get($param['group_id']);
    	if(!empty($data['header'])){
    		$data['header']['content'] = json_decode($data['header']['content'],true);
    	}
    	if(!empty($data['footer'])){
    		$data['footer']['content'] = json_decode($data['footer']['content'],true);
    	}
        $url = 'http://'.$_SERVER['SERVER_NAME'].'/portal/img/index/group_id/'.$param['group_id'];
        $path = $lEwm->index($param['group_id'],$url);//获取二维码
        
    	//dump($data['template']['progress']);
        $this->assign('path',cmf_get_image_url($path));
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    //设置套件ID
    public function set(){
        $param = $this->request->param();
        //halt($param['status']);
        $rs = TemplateGroupModel::where('group_id',$param['group_id'])->update(['status'=>$param['status']]);
        if($rs){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
}
